package com.zdp.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * @author sesshomaru
 * @date 2021/7/23
 */
@FeignClient(name = "stock-server", fallback = StockFeignClientFallback.class, configuration = FeignConfiguration.class)
public interface StockFeignClient {

    /**
     * http://stock-server/stock/reduce/{productId
     * @param productId
     * @return
     */
    @GetMapping("/stock/reduce/{productId}")
    String reduce(@PathVariable("productId") Integer productId);
}

/**
 * 官网的书写格式，不过也可以单独将StockFeignClientFallback注册为一个Bean
 */
class FeignConfiguration{
    @Bean
    public StockFeignClientFallback stockFeignClientFallback(){
        return new StockFeignClientFallback();
    }
}

/**
 * StockFeignClientFallback，只要调用远端接口发生异常都会进入这个重写的方法中
 */
class StockFeignClientFallback implements StockFeignClient{
    @Override
    public String reduce(Integer productId) {
        return "库存服务发生了限流、降级";
    }
}
